package com.hzdl.car.util;

import com.hzdl.car.bean.Customer;
import com.hzdl.car.bean.Rent;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class Common {


    public static ByteArrayOutputStream print(List<Customer> list ){

        // 创建工作表格
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 根据表格对象创建sheet
        HSSFSheet sheet = workbook.createSheet("客户信息一览");

        sheet.setColumnWidth(1, 18 * 256);//设置第一列的宽度是18个字符宽度

        // 创建第一行
        HSSFRow row1 = sheet.createRow(0);
        // 创建第一列
        HSSFCell row1_cell1 = row1.createCell(0);
        // 设置值
        row1_cell1.setCellValue("客户信息一览");

        // 创建样式
        HSSFFont font = workbook.createFont();//创建字体样式
        font.setFontName("华文彩云");// 字体名称
        font.setFontHeightInPoints((short)28);//设置字号
        font.setColor(HSSFColor.RED.index);//设置字体颜色

        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setFont(font);//修改字体样式
        cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中

        row1_cell1.setCellStyle(cellStyle);//修改第一行，第一列样式

        // 合并第一行第一列到底七列
        CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, 6);
        sheet.addMergedRegion(cellRangeAddress);




        // 创建第二行
        HSSFRow row2 = sheet.createRow(1);
        HSSFCell row2_cell1 = row2.createCell(0);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        String format1 = simpleDateFormat.format(new Date());
        row2_cell1.setCellValue("总条数："+list.size()+"条   导出时间："+format1);
        // 创建样式
        HSSFFont font2 = workbook.createFont();
        font2.setFontName("宋体");// 字体名称
        font2.setFontHeightInPoints((short)12);//设置字号
        font2.setColor(HSSFColor.BLUE.index);//设置字体颜色

        HSSFCellStyle cellStyle2 = workbook.createCellStyle();
        cellStyle2.setFont(font2);
        cellStyle2.setAlignment(HorizontalAlignment.CENTER);//水平居中
        cellStyle2.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中

        row2_cell1.setCellStyle(cellStyle2);

        // 合并第一行第一列到底七列
        CellRangeAddress cellRangeAddress2 = new CellRangeAddress(1, 1, 0, 6);
        sheet.addMergedRegion(cellRangeAddress2);


        // 第三行
        String[] title = {"身份证号","客户姓名","性别","地址","联系电话","职位","创建时间"};
        HSSFRow row3 = sheet.createRow(2);

        for (int i = 0 ;i < title.length;i++) {
            HSSFCell row3_cell1 = row3.createCell(i);
            row3_cell1.setCellValue(title[i]);

            HSSFFont font3 = workbook.createFont();
            font3.setFontName("宋体");// 字体名称
            font3.setFontHeightInPoints((short) 12);//设置字号
            font3.setColor(HSSFColor.BLACK.index);//设置字体颜色
            HSSFCellStyle cellStyle3 = workbook.createCellStyle();
            cellStyle3.setFont(font3);
            cellStyle3.setAlignment(HorizontalAlignment.CENTER);//水平居中
            cellStyle3.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中
            row3_cell1.setCellStyle(cellStyle3);
        }

        int rownum=2;
        //数据库查询数据显示
        for (Customer customer:list){
            rownum++;
            HSSFRow rowx = sheet.createRow(rownum);
            //身份证
            HSSFCell rowx_cell1 =  rowx.createCell(0);
            rowx_cell1.setCellValue(customer.getIdentity());
            //客户姓名
            HSSFCell rowx_cell2 =  rowx.createCell(1);
            rowx_cell2.setCellValue(customer.getCustname());
            //性别
            HSSFCell rowx_cell3 =  rowx.createCell(2);
            rowx_cell3.setCellValue(customer.getSex()== 0 ?"女":"男");
            //地址
            HSSFCell rowx_cell4 =  rowx.createCell(3);
            rowx_cell4.setCellValue(customer.getAddress());
            //联系方式
            HSSFCell rowx_cell5 =  rowx.createCell(4);
            rowx_cell5.setCellValue(customer.getPhone());
            //职位
            HSSFCell rowx_cell6 =  rowx.createCell(5);
            rowx_cell6.setCellValue(customer.getCareer());
            //创建时间
            HSSFCell rowx_cell7 =  rowx.createCell(6);
            SimpleDateFormat simpleDateFormat1=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
            String format = simpleDateFormat1.format(customer.getCreatetime());
            rowx_cell7.setCellValue(format);

        }





        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
           workbook.write(stream);
            return  stream;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return  null;

    }



    public static ByteArrayOutputStream print2(Rent rent, String  customerName) {
        InputStream is = Common.class.getClassLoader().getResourceAsStream("czd-templet.xls");
        try {
            HSSFWorkbook workbook=new HSSFWorkbook(is);
            HSSFSheet sheet = workbook.getSheet("出租单");
            HSSFRow row1 = sheet.getRow(0);
            HSSFCell row1_cell1 = row1.getCell(0);
            row1_cell1.setCellValue(customerName+"的出租单");

            HSSFRow row2 = sheet.getRow(1);
            HSSFCell row2_Cell2 = row2.getCell(1);
            row2_Cell2.setCellValue(rent.getRentid());

            HSSFRow row3 = sheet.getRow(2);
            HSSFCell row3_Cell2 = row3.getCell(1);
            row3_Cell2.setCellValue(customerName);

            HSSFRow row4 = sheet.getRow(3);
            HSSFCell row4_Cell2 = row4.getCell(1);
            row4_Cell2.setCellValue(rent.getBegindate());

            HSSFRow row5 = sheet.getRow(4);
            HSSFCell row5_Cell2 = row5.getCell(1);
            row5_Cell2.setCellValue(rent.getCarnumber());

            HSSFCell row3_Cell5 = row3.getCell(4);
            row3_Cell5.setCellValue(rent.getIdentity());

            HSSFCell row4_Cell5 = row4.getCell(4);
            row4_Cell5.setCellValue(rent.getReturndate());

            HSSFCell row5_Cell5 = row5.getCell(4);
            row5_Cell5.setCellValue(rent.getPrice().toString());

            HSSFRow row6 = sheet.getRow(5);
            HSSFCell row6_Cell5 = row6.getCell(4);
            row6_Cell5.setCellValue(new Date());



            HSSFRow row7 = sheet.getRow(6);
            HSSFCell row7_Cell5 = row7.getCell(4);
            row7_Cell5.setCellValue(rent.getOpenname());

            BufferedImage image = ZXingUtil.createZXingCode(rent.getRentid(), 120, 120);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ImageIO.write(image,"JPEG",bos);
            HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
            /**
             * 参数4  设置图片的平铺程度  最大值是255  255代表铺满当前单元格 小于255就铺不满
             * 参数5  列的开始坐标
             * 参数6  行的开始坐标
             * 参数7  列的结束坐标
             * 参数8  行的结束坐标
             */
            HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 255, (short) 4, 1, (short) 5, 1);
            anchor.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);
            patriarch.createPicture(anchor,workbook.addPicture(bos.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));


            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            workbook.write(stream);


            return stream;

        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;

    }



}
